/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie.test;

import com.rie.dao.DAO;
import com.rie.model.Monitoring;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Habib, Arnaud
 */
@WebServlet(name = "controleurHistorique", urlPatterns = {"/controleurHistorique"})
public class ControleurHistorique extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet displayListClient</title>"); 
            out.println(" <link href='bootstrap/css/bootstrap-responsive.min.css' rel='stylesheet' type='text/css'>\n" +
"        <link href='bootstrap/css/bootstrap.css' rel='stylesheet' type='text/css'>\n" +
"        <link href='bootstrap/css/bootstrap-responsive.css' rel='stylesheet' type='text/css'>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet displayListClient at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
            String filter =null;
            if(request.getParameter("existingServer")==null){
                filter="*";
            }else{
                filter=request.getParameter("existingServer");
            }
          DAO reportDao = new DAO();
          List<Monitoring> lr = reportDao.getMonitoring(filter);
          PrintWriter out = response.getWriter();
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Historique</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Historique du serveur : "+filter+"</h1>");
            out.println("<form name='delete' method='get' action='./controleurDelete'>");
            out.println("<input type='hidden' name='ipserver' value='"+filter+"'>");
            out.println("<input type='submit' value='Supprimer Tout' />");
             out.println("</form>");
            if(lr.size()>0){
                out.println("<table>");
                 out.println("<tr><th>Date</th><th>Serveur</th><th>Mémoire</th><th>CPU</th><th>Processus</th><th>Détails</th></tr>");
                for(Monitoring r : lr){
                    out.print("<tr><td>"+r.getDate()+"</td><td>"+r.getServer()+"</td>");
                    if(r.getId_memory()>0){
                        out.print("<td>Oui</td>");
                    }else{
                        out.print("<td>Non</td>");
                    }
                    if(r.getId_cpu()>0){
                        out.print("<td>Oui</td>");
                    }else{
                        out.print("<td>Non</td>");
                    }
                    if(r.getProcess()>0){
                        out.print("<td>Oui</td>");
                    }else{
                        out.print("<td>Non</td>");
                    }
                 out.println("<td><a href='./controleurReport?id="+r.getId()+"'>Voir</a></td></tr>");               
                }
            }else{
            out.println("Aucun résultat à votre recherche.<br/>");   
            }
            out.println("<a href='./index.jsp'>Retour à la page de recherche</a>"); 
            out.println("</body>");
            out.println("</html>");
            out.close();      
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
